package com.css.fxfzdpzs.modules.xczh.controller;

import com.css.fxfzdpzs.base.exception.CustomException;
import com.css.fxfzdpzs.base.response.RestResponse;
import com.css.fxfzdpzs.modules.xczh.dto.DisasterDamageDTO;
import com.css.fxfzdpzs.modules.xczh.entity.first.DisasterAssessData;
import com.css.fxfzdpzs.modules.xczh.entity.first.DisasterDamage;
import com.css.fxfzdpzs.modules.xczh.entity.first.DisasterDamageData;
import com.css.fxfzdpzs.modules.xczh.entity.first.GradeDisasterNum;
import com.css.fxfzdpzs.modules.xczh.entity.second.*;
import com.css.fxfzdpzs.modules.xczh.service.XczhService;
import com.css.fxfzdpzs.modules.xczh.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 地震灾害现场调查与损失评估
 */
@Slf4j
@RestController
@RequestMapping("/xczh")
public class XczhController {
    @Autowired
    private XczhService xczhService;

    //一级表单(全国灾损展示）

    //全国五级以上地震数量和灾害评估数量
    @GetMapping("/getDisasterNum")
    public RestResponse getDisasterNum() {
        RestResponse restResponse = null;
        try {
            GradeDisasterNum gradeDisasterNum=xczhService.getDisasterNum();
            restResponse = RestResponse.succeed(gradeDisasterNum);
        } catch (Exception e) {
            String errormessage = "查询国五级以上地震数量和灾害评估数量失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //灾评次数前五省份
    @GetMapping("/getDamageNum")
    public RestResponse getDamageNum() {
        RestResponse restResponse = null;
        try {
            List<DisasterNumVo> damageNums = xczhService.getDamageNum();
            restResponse = RestResponse.succeed(damageNums);
        } catch (Exception e) {
            String errormessage = "查询灾损数量前五省份失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    //近五次地震灾害各类损失占比
    @GetMapping("/getLossProportion")
    public RestResponse getLossProportion() {
        RestResponse restResponse = null;
        try {
            List<LossProportionVo> lossProportionList=xczhService.getLossProportion();
            restResponse = RestResponse.succeed(lossProportionList);
        }catch (CustomException e) {
            log.error(e.getMessage());
            restResponse = RestResponse.fail(e.getMessage());
        } catch (Exception e) {
            String errormessage = "查询近五次地震灾害各类损失占比失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //灾评单位统计
    @GetMapping("/getUnitCount")
    public RestResponse getUnitCount() {
        RestResponse restResponse = null;
        try {
            List<DisasterAssessData> count=xczhService.getUnitCount();
            restResponse = RestResponse.succeed(count);
        } catch (Exception e) {
            String errormessage = "查询灾评单位统计失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //灾损值前五省份
    @GetMapping("/getDisasterValue")
    public RestResponse getDisasterValue() {
        RestResponse restResponse = null;
        try {
            List<DisasterValueVo> disasterValues=xczhService.getDisasterValue();
            restResponse = RestResponse.succeed(disasterValues);
        } catch (Exception e) {
            String errormessage = "查询灾损值前五省份失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //地震灾害损失评估访问量统计
    @GetMapping("/getVisitNum")
    public RestResponse getVisitNum(@RequestParam String system,@RequestParam(required = false) Integer num) {
        RestResponse restResponse = null;
        try {
            num= num==null?3:num;
            Map<String,Object> map=xczhService.getVisitNum(system,num);
            restResponse = RestResponse.succeed(map);
        } catch (Exception e) {
            String errormessage = "查询地震灾害损失评估访问量统计失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //**县历年来地震灾损情况表
    @GetMapping("/getSituation")
    public RestResponse getSituation() {
        RestResponse restResponse = null;
        try {
            List<DisasterDamage> list=xczhService.getSituation();
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询**县历年来地震灾损情况表失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     1）全国地震灾害损失评估结果
     表名（英文名）：xczh_disaster_image
     *
     * @return
     */
    @GetMapping("/getDisasterImage")
    public RestResponse getDisasterImage(String iaId,String intensity) {
        RestResponse restResponse = null;
        try {
            List<XcDisasterImage> list= xczhService.getDisasterImage(iaId,intensity);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    //全国地震灾害损失评估结果现场数据占比
    @GetMapping("/getXcProportion")
    public RestResponse getXcProportion(String iaId) {
        RestResponse restResponse = null;
        try {
            XcDataNum xcDataNum=xczhService.getXcProportion(iaId);
            restResponse = RestResponse.succeed(xcDataNum);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    //全国地震灾害损失评估结果
    @GetMapping("/getXcEstimateData")
    public RestResponse getXcEstimateData(String iaId) {
        RestResponse restResponse = null;
        try {
            XcDataNum xcDataNum=xczhService.getXcProportion(iaId);
            restResponse = RestResponse.succeed(xcDataNum);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //全国地震灾害损失 地图数据
    @GetMapping("/getMapDate")
    public RestResponse getMapDate() {
        RestResponse restResponse = null;
        try {
            XcMapEntity entity=xczhService.getMapDate();
            restResponse = RestResponse.succeed(entity);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //全国地震灾害损失 地图数据  根据县Code 查询县历年来地震信息
    @GetMapping("/getCountyDisasterByCountyCode")
    public RestResponse getCountyDisasterByCountyCode(String countyCode) {
        RestResponse restResponse = null;
        try {
            List<XcCountySituation> list=xczhService.getCountyDisasterByCountyCode(countyCode);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }








    @GetMapping("/getSecondMapDate")
    public RestResponse getSecondMapDate(
            /*@RequestParam String longitude,
    @RequestParam String latitude,
    @RequestParam String magnitude,
    @RequestParam String eventTime*/) {
        RestResponse restResponse = null;
        try {
//           XcSecondMapEntity entity=xczhService.getSecondMapDate();
             XcSecondMapEntity entity=xczhService.getEarthquakeEventInfo("","","","");
            restResponse = RestResponse.succeed(entity);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
